package com.wthink.newMain.newBusiness.inner.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.service.jt808.JT_0B09
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class NewBusinessUpViewmodel : ViewModel(){

    var busLineNo = -1

    var business = -1

    fun updateBusiness(businessType: String,success:(Boolean)->Unit) {
        //queryBusStateUntilSuccess()
        when (businessType) {
            "请求排班" -> {
                business = 1
            }
            "请求交班" -> {
                business = 2
            }
            "请求加油" -> {
                business = 3
            }
            "请求加气" -> {
                business = 4
            }
            "请求充电" -> {
                business = 5
            }
            "请求维修" -> {
                business = 6
            }
            "请求包车" -> {
                business = 7
            }
            "其他请求" -> {
                business = 8
            }
            "退出运营" -> {
                business = 9
            }
            "手动开始" -> {
                business = 10
            }
            "手动结束" -> {
                business = 11
            }
        }
        var array: Array<String> = arrayOf(
            "请求排班", "请求交班", "请求加油", "请求加气", "请求充电",
            "请求维修", "请求包车", "其他请求","退出运营","手动开始","手动结束"
        )

        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.send808(JT_0B09(busLineNo, "12345", business))
            withContext(Dispatchers.Main) {
                if (ret.success) {
                    toast(BusApplication.busContext, "上报成功")
                    success(ret.success)
                } else {
                    toast(BusApplication.busContext, ret.err.toString())
                }
            }
        }
    }
}